/*
 * Copyright (C) 2017-2017 SonarSource SA
 * All rights reserved
 * mailto:info AT sonarsource DOT com
 */
import React from 'react';
import MeasuresHistory from './MeasuresHistory'
import {translate} from '../common/l10n.js'
import {findVersionsAndMeasures} from '../api.js'
/*!function(e){
  function t(r){
 if(n[r])return n[r].exports;
 var a=n[r]={exports:{},id:r,loaded:!1};
 return e[r].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}
 var n={};return t.m=e,t.c=n,t.p="",t(0)}
 ([function(e,t,n){e.exports=n(10)},function(e,t){e.exports=React},
 function(e,t,n){"use strict";function r(e){
 return(0,l.getJSON)
 ("/api/qualityprofiles/search").then(function(e){return e.profiles.length})}
 function a(e){
 return(0,l.getJSON)("/api/qualitygates/list").then(function(e)
 {return e.qualitygates.length})}function o(e)
 {return(0,l.getJSON)("/api/issues/search").then(function(e){return e.total})}
 function s(e)
 {return(0,l.getJSON)("/api/projects/search").then(function(e)
 {return e.components.length})}function i(e)
 {return(0,l.getJSON)("/api/project_analyses/search",
 {project:e.key,p:1,ps:500}).then(function(t)
 {var n=t.analyses.length;if(n>0)return(0,l.getJSON)("/api/measures/search_history",
 {component:
 e.key,metrics:"alert_status,bugs,vulnerabilities,code_smells,reliability_rating,security_rating,
 sqale_rating",ps:1e3}).then(function(e){for(var r=[],a=0,o=0;o<n;o++)
 for(var s=t.analyses[o],i=0;i<s.events.length;i++)
 if("VERSION"===s.events[i].category)
 {for(var l={version:s.events[i].name,alert_status:"",bugs:"0",vulnerabilities:"0",code_smells:"0",reliability_rating:"",security_rating:"",sqale_rating:""},u=7,c=0;c<u;c++)for(var f=0;f<e.measures[c].history.length;f++)e.measures[c].history[f].date===t.analyses[o].date&&
 */
 export default class VersionsMeasuresHistoryApp extends React.PureComponent {

  state = {
    data: []
  };

  componentDidMount() {
    findVersionsAndMeasures(this.props.project).then(
      (valuesReturnedByAPI) => {
        this.setState({
          data: valuesReturnedByAPI
        });
      }
    );
  }

  render() {
    // Data Gathered: {JSON.stringify(this.state.data)}
    return (
      <div className="page page-limited">
        <table className="data zebra">
          <thead><tr className="code-components-header">
            <th className="thin nowrap text-left code-components-cell">Version</th>

            <th className="thin nowrap text-center code-components-cell">Quality Gate</th>

            <th className="thin nowrap text-right code-components-cell">{translate('issue.type.BUG.plural')}</th>
            <th className="thin nowrap text-right code-components-cell">Reliability Rating</th>

            <th className="thin nowrap text-right code-components-cell">Vulnerabilities</th>
            <th className="thin nowrap text-right code-components-cell">Security Rating</th>

            <th className="thin nowrap text-right code-components-cell">Code Smells</th>
            <th className="thin nowrap text-right code-components-cell">Maintainability Rating</th>
          </tr></thead>
          <tbody>
          {this.state.data.map(
              (value,idx) =>
              <MeasuresHistory
                measure={value}
                key={idx}
              />
              )
          }
          </tbody>
        </table>
      </div>
    );
  }
}
